package com.lz.homedecoration.controller;

import com.lz.homedecoration.common.bean.PageResult;
import com.lz.homedecoration.common.controller.BaseController;
import com.lz.homedecoration.common.request.PageReq;
import com.lz.homedecoration.common.response.ResultRes;
import com.lz.homedecoration.entity.User;
import com.lz.homedecoration.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lz.homedecoration.common.util.CommonLogUtil;
@RestController
@RequestMapping("/user")
@Api(value = "用户")
public class UserController extends BaseController<User> {

    @Autowired
    UserService userService;

    @ApiOperation(value = "获取用户列表", notes = "获取用户列表")
    @Override
    public ResultRes list() {
        return ResultRes.success(userService.list());
    }

    @ApiOperation(value = "获取用户信息", notes = "根据用户id获取用户信息")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long", paramType = "path")
    @Override
    public ResultRes get(@PathVariable Long id) {
        User user = userService.get(id);
        return ResultRes.success(user);
    }

    @Override
    public ResultRes add(@RequestBody User user) {
        userService.add(user);
        return ResultRes.success();
    }

    @Override
    public ResultRes update(@RequestBody User user) {
        userService.update(user);
        return ResultRes.success();
    }

    @Override
    public ResultRes delete(@PathVariable Long id) {
       int count = userService.delete(id);
       if(count !=0){
           return ResultRes.success();
       }else {
           CommonLogUtil.logger.info("---------没有相关id: "+id+"的数据-------------");
           throw new RuntimeException("没有相关id: "+id+"的数据");
       }
    }

    @Override
    public ResultRes pageList(@RequestBody PageReq pageReq) {
        PageResult<User> pageResult = userService.pageList(pageReq);
        return ResultRes.success(pageResult);
    }

}
